package com.数据库MYSQL.JDBC连接;

import java.sql.*;

public class JDBCDemo5 {
    public static void main(String[] args) {
        Statement statement = null;
        Connection connection = null;
        ResultSet rs = null;
        //1.注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.定义sql id自动增长,使用null占位符
            String sql = "select * from account";
            //获取connection对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "nddwm");
            //4.获取执行sql的对象
            statement = connection.createStatement();
            //5.执行sql
            rs = statement.executeQuery(sql);
            //6.处理结果
            //6.1让游标向下移动一行
            while (rs.next()) {
                //6.2获取数据
                int anInt = rs.getInt(1);
                String name = rs.getString("name");
                double balance = rs.getDouble("balance");
                System.out.println(anInt + "..." + name + "..." + balance);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            //避免空指针异常
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


        }
    }
}
